/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb_stateless;

import java.io.IOException;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;

/**
 *
 * @author Administrator
 */
public abstract class AbstractFacade<T> {

    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

     public int getMaxID(String biao,String id){//---获取表YhYhcpwh中最大的ID
     String str="select max(s."+id+") from "+biao+" s";
     int maxid=0;
     if(getEntityManager().createQuery(str).getSingleResult()!=null)
       maxid=(Integer)getEntityManager().createQuery(str).getSingleResult();
      return maxid;
     }
    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<T> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    public List<T> findAll(String str) {
        return getEntityManager().createQuery(str).getResultList();
    }

    public List<T> findRange(int[] range, String str) {
        javax.persistence.Query q = getEntityManager().createQuery(str);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        List list = q.getResultList();//0719
        for (Object s : list) {
            getEntityManager().refresh(s);//0719刷新实体让页面与数据库保持同步
        }
        return list;
    }

    public int count(String str) {
        javax.persistence.Query q = getEntityManager().createQuery(str);
        return ((Long) q.getSingleResult()).intValue();
    }

    public double sum(String str) {
        javax.persistence.Query q = getEntityManager().createQuery(str);
        String value = "0";
        if (q.getSingleResult() != null) {
            value = q.getSingleResult().toString();
        }
        return Double.valueOf(value);
    }

    public void resetURL(String link) throws IOException {
        String projectPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/faces/";
        int index = link.indexOf("?");
        if(index > -1){
          FacesContext.getCurrentInstance().getExternalContext().redirect(projectPath + link.substring(0, index) + ".xhtml" + link.substring(index));
        }else{
        FacesContext.getCurrentInstance().getExternalContext().redirect(projectPath + link + ".xhtml");
        }
    }
}
